/**
 * @author zhounan 
 * @date 2024年7月9日
 * @version V1.0
 */
package org.zn.etl.job.handler.extend;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.stereotype.Component;
import org.zn.datax.common.entity.ColumnInfo;

import com.google.common.collect.Lists;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhounan 
 * @date 2024年7月9日
 * @version V1.0
 */
@Component("reginfoTrans")
@Slf4j
public class ReginfoTrans extends CitiyTrans {

	private Map<String, List<BusTypeMappinng>> mapppingData;
	
	@Data
	@AllArgsConstructor
	static class BusTypeMappinng {
		
		private String origBusinessType;
		private String origBusType;
		private String targBusinessType;
		private String targSubbusinessType;
		private String targBusinessName;
		
	}
	
	@PostConstruct
	public void init() {
		
		mapppingData=new HashMap<>();
		mapppingData.put("N10000",Lists.newArrayList( new BusTypeMappinng("N10000", null, "N00000", "701",null)));
		mapppingData.put("N10010",Lists.newArrayList( new BusTypeMappinng("N10010", null, "N00000", "702",null)));
		mapppingData.put("N01600",Lists.newArrayList( new BusTypeMappinng("N01600", null, "N00000", "709","留置权")));
		mapppingData.put("N10020",Lists.newArrayList( new BusTypeMappinng("N10020", null, "N00000", "709","动产信托")));
		
		mapppingData.put("N00000",Lists.newArrayList( 
		 new BusTypeMappinng("N00000", "4", "N00000", "709","贵金属质押")
		,new BusTypeMappinng("N00000", "1", "N00001", "709","非上市公司股权质押")
		,new BusTypeMappinng("N00000", "5", "N00000", "709",null)
		,new BusTypeMappinng("N00000", "2", "N00000", "703",null)
		,new BusTypeMappinng("N00000", "6", "N00000", "704",null)
		,new BusTypeMappinng("N00000", "7", "N00000", "705",null)));
		
		super.init();
		
		log.info("ReginfoTrans citiy map init finish");
		
	}
	
	
	@Override
	public List<Map<String, Object>> transform(List<Map<String, Object>> data,
			List<Pair<ColumnInfo, ColumnInfo>> columnMappingPairs) {
		// TODO Auto-generated method stub
		String key= getUseKey(data.get(0));
		
		for (Map<String, Object> obj : data) {
			
			try {
			
			String bt= (String) obj.get("businesstype");
			String bustype= (String) obj.get("bustype");
			//String bustypename= (String)   obj.get("bustypename");
			
			obj.put("subbusinesstype",null);
			
			//if("N01600".equals(bt)||"N10020".equals(bt)||"N00000".equals(bt)||"N10010".equals(bt)) {
				//System.out.println(bt+"-"+bustype);
			//}

			List<BusTypeMappinng> mappings=mapppingData.get(bt);
			
			if(mappings !=null) {
				
				if(mappings.size()==1) {
					
					obj.put("businesstype", mappings.get(0).getTargBusinessType());
					obj.put("subbusinesstype", mappings.get(0).getTargSubbusinessType());
					
					if(StringUtils.isNotEmpty(mappings.get(0).getTargBusinessName()) ) {
						obj.put("bustypename", mappings.get(0).getTargBusinessName());
					}
					
				}
				else {
					
					for (BusTypeMappinng mp : mappings) {
						
						if( Objects.equals(mp.getOrigBusType(), bustype) ) {
							obj.put("businesstype", mp.getTargBusinessType());
							obj.put("subbusinesstype", mp.getTargSubbusinessType());
							
							if(StringUtils.isNotEmpty(mp.getTargBusinessName()) ) {
								obj.put("bustypename", mp.getTargBusinessName());
							}
							
							break; 
							
						}
						
					}
				}
			}
			
			String useK=getUseKey(data.get(0));
			
			if(key==null) {
				log.info("OrguserInfoTrans: No citiy UseKey");
				return data;
			}
			transCity(obj, useK);
			
			} catch (Exception e) {
				log.error("OrguserInfoTrans error, Citiy UseKey:{}  ,data:{}",key,obj,e);
			}
		
		}
		return data;
	}

}
